package com.txsj.scan.widget.popup;

/**
 * Created by 徐嘉健 on 2018/11/30.
 */
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.utils.ToastUtils;
import com.txsj.scan.utils.Trace;


/**
 * Created by wwb on 2017/6/2.
 */

public class PopupReject {

    TextView tv_title;
    EditText et_store_setting;
    Button btn_save, btn_cancel;
    Context mContext;
    PopupWindow mPopup;
    LinearLayout lin_popup_root;
    View rootView;

    public PopupReject(Context context, View view) {
        this.mContext = context;
        this.rootView = view;
    }

    public PopupReject builder() {
        try {
            LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            View v = layoutInflater.inflate(R.layout.popup_reject, null);
            btn_save = (Button) v.findViewById(R.id.btn_save);//保存
            btn_cancel = (Button) v.findViewById(R.id.btn_cancel);//取消
            tv_title = (TextView) v.findViewById(R.id.tv_title);//

            et_store_setting = (EditText) v.findViewById(R.id.et_store_setting);//
            lin_popup_root = (LinearLayout) v.findViewById(R.id.lin_popup_root);

            // 为自定义的对话框设置自定义布局
            mPopup = new PopupWindow(v, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            mPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            mPopup.setFocusable(true);
            mPopup.setBackgroundDrawable(new BitmapDrawable());
            // 不允许在外点击消失
            mPopup.setOutsideTouchable(true);
            mPopup.setAnimationStyle(R.style.anim_popu);
            lin_popup_root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPopup.dismiss();
                }
            });
            btn_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPopup.dismiss();
                }
            });
        } catch (Exception e) {
            Trace.e("异常=" + e);
        }
        return this;
    }

    /**
     * 保存
     *
     * @return
     */
    public PopupReject setSave(final IOnSaveClick listener) {
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!et_store_setting.getText().toString().equals("")){
                    mPopup.dismiss();
                    listener.onSaveClick(et_store_setting.getText().toString().trim());
                }else {
                    ToastUtils.showToast("请输入驳回原因");
                }

            }
        });
        return this;
    }

    /**
     * 设置标题
     *
     * @return
     */
    public PopupReject setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            tv_title.setText(title);
        }
        return this;
    }

    public PopupReject setEditContent(String content) {
        if (!TextUtils.isEmpty(content)) {
            et_store_setting.setText(content);
            et_store_setting.setSelection(content.length());
        }
        return this;
    }



    /**
     * 显示PopupWindow
     *
     * @return
     */
    public PopupReject show() {
        try {
            mPopup.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
            InputMethodManager inputManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            Trace.e("异常=" + e);
        }
        return this;
    }


    public interface IOnSaveClick {
        void onSaveClick(String remark);
    }

}
